﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using FlexControls;
/*
 * 
 * try
            {
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
 * 
 * 
 */
namespace KHOALUAN
{
    class clsMain
    {
        public static clsFlex cls = new clsFlex();
        public static int FormHocKy = 0;
        public static int FormMonHoc = 1;
        public static int FormGridView = 2;
        public static int FormLopHoc = 3;
        public static int FormChucDanh = 4;
        public static int FormGiaoVien = 5;
        public static int FormKhoa = 6;
        public static int FormCaiDatCTHoc = 7;
        //Only for Form Dang Ky Day Hoc
        public static string strGiangVien="";
        public static string strGiangVienCode = "";
        public struct FormCotrol
        {
           // public Form
        };
        public struct XMLData
        {
            
            public int SoColumn;
            public string TableName;
            public string[] ColumnName;
            public string[] Names;
            public string[] Value;
        };
        public const string DATA_PATH = "..\\..\\DATA\\";
        public static void CreateXMLTable(string strTableName,XMLData xmlD)
        {
            XmlTextWriter xWrite;

            xWrite = new XmlTextWriter(DATA_PATH + strTableName + ".xml", System.Text.Encoding.UTF8);

            xWrite.WriteStartDocument(true);
            xWrite.Formatting = Formatting.Indented;
            xWrite.Indentation = 3;
            xWrite.WriteStartElement(strTableName +"s");
            xWrite.WriteStartElement("ColumnNames");
            for (int i = 0; i < xmlD.SoColumn; i++)
            {
                xWrite.WriteStartElement(xmlD.Names[i]);
                xWrite.WriteString(xmlD.ColumnName[i]);
                xWrite.WriteEndElement();
            }
            xWrite.WriteEndElement();
            xWrite.WriteEndElement();
            
            xWrite.WriteEndDocument();

            xWrite.Flush();
            xWrite.Close();


        }
        public static bool FindAElement(string strTableName,string strColumnName,string strElement)
        {
            XElement xelement = XElement.Load(DATA_PATH+strTableName +".xml");
            var addresses = from address in xelement.Elements(strTableName)
                            where (string)address.Element(strColumnName) == strElement
                            select address;
            foreach(XElement xEle in addresses)
            {
                return true;
            }
                return false;
 
        }
        public static void InsertElement(System.Windows.Forms.Form frm,string strTableName,XMLData xmlD)
        {
            for (int i = 0; i < frm.Controls.Count; i++)
            {
                if (frm.Controls[i].Tag != null)
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {

                        if (xmlD.Names[j] == frm.Controls[i].Tag.ToString())
                            xmlD.Value[j]=frm.Controls[i].Text ;
                    }
            }
            if (FindAElement(strTableName, xmlD.Names[0], xmlD.Value[0]) != true)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(DATA_PATH+ strTableName + ".xml");
                XmlElement xEle = xmlDoc.CreateElement(strTableName);
                for (int i = 0; i < xmlD.SoColumn; i++)
                {

                    XmlElement xEleChild = xmlDoc.CreateElement(xmlD.Names[i]);
                    xEleChild.InnerText = xmlD.Value[i].ToString();
                    xEle.AppendChild(xEleChild);
                }
                xmlDoc.DocumentElement.AppendChild(xEle);
                xmlDoc.Save(DATA_PATH+ strTableName + ".xml");
            }
        }
        public static void KhoiTaoXMLTable(string name,ref clsMain.XMLData xml,int intSoColumn)
        {
            xml.TableName = name;
            xml.SoColumn = intSoColumn;
            
            xml.ColumnName= new string[intSoColumn];
            xml.Names = new string[intSoColumn];
            xml.Value = new string[intSoColumn];
            for(int i=0;i< intSoColumn;i++)
            {
                xml.Value[i]="Empty";
                
            }
        }
        public static void LoadData(System.Windows.Forms.Form frm,XMLData a)
        {
            
            for (int i = 0; i < frm.Controls.Count; i++)
            {
                if(frm.Controls[i].Tag!=null)
                    for(int j=0;j<a.SoColumn;j++)
                    {
                        
                        if (a.Names[j] == frm.Controls[i].Tag.ToString())
                            frm.Controls[i].Text = a.Value[j];
                    }
            }
        }
        public static void SaveData(System.Windows.Forms.Form frm, XMLData a)
        {
            for (int i = 0; i < frm.Controls.Count; i++)
            {
                if (frm.Controls[i].Tag != null)
                    for (int j = 0; j < a.SoColumn; j++)
                    {

                        if (a.Names[j] == frm.Controls[i].Tag.ToString())
                        {
                            a.Value[j] = frm.Controls[i].Text;
                            frm.Controls[i].Text = "";
                        }
                    }
            }
        }
        public static void ReadXML(string strTableName,XMLData a)
        {
            XElement xelement = XElement.Load(DATA_PATH + strTableName + ".xml");
            IEnumerable<XElement> Columns = xelement.Elements();
            foreach (var col in Columns)
            {
                if (col.Name != "ColumnNames")
                {
                    for (int j = 0; j < a.SoColumn; j++)
                    {
                        a.Value[j] = col.Element(a.Names[j]).Value;
                    }
                }
            }
        }
        public static void AddDataToDataGridView(string strTableName,ref System.Windows.Forms.DataGridView drv,XMLData xmlD)
        {
            XElement xelement = XElement.Load(DATA_PATH + strTableName + ".xml");
            IEnumerable<XElement> Columns = xelement.Elements();
            // Read the entire XML
            //int j = 0;
            System.Data.DataTable dt = new System.Data.DataTable();
            foreach (var col in Columns)
            {
                if (col.Name == "ColumnNames")
                {
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {
                        dt.Columns.Add(col.Element(xmlD.Names[j]).Value, typeof(string));

                    }
                }
                else 
                {
                    System.Data.DataRow row = dt.NewRow();
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {
                        row[j] = col.Element(xmlD.Names[j]).Value;
                        
                    }
                    dt.Rows.Add(row);
                }
            }
            
            drv.DataSource = dt;
            for (int i = 0; i < xmlD.SoColumn; i++)
            {
                drv.Columns[i].Tag = xmlD.Names[i];
            }
        }
        public static void AddDataToDataGridViewMonHoc(string strTableName, ref System.Windows.Forms.DataGridView drv, XMLData xmlD)
        {
            XElement xelement = XElement.Load(DATA_PATH + strTableName + ".xml");
            IEnumerable<XElement> Columns = xelement.Elements();
            // Read the entire XML
            //int j = 0;
            System.Data.DataTable dt = new System.Data.DataTable();
            foreach (var col in Columns)
            {
                if (col.Name == "ColumnNames")
                {
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {
                        dt.Columns.Add(col.Element(xmlD.Names[j]).Value, typeof(string));

                    }
                }
                else
                {
                    System.Data.DataRow row = dt.NewRow();
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {
                        row[j] = col.Element(xmlD.Names[j]).Value;

                    }
                    dt.Rows.Add(row);
                }
            }

            drv.DataSource = dt;
            for (int i = 0; i < xmlD.SoColumn; i++)
            {
                drv.Columns[i].Tag = xmlD.Names[i];
            }
        }
        public static void AddDataToControls(System.Windows.Forms.Form frm, System.Windows.Forms.DataGridView drv,XMLData xmlD)
        {
            
            for (int i = 0; i < frm.Controls.Count; i++)
            {
                if (frm.Controls[i].Tag != null)
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {

                        if (drv.Columns[drv.SelectedCells[j].ColumnIndex].Tag.ToString() == frm.Controls[i].Tag.ToString())
                            frm.Controls[i].Text = drv.SelectedCells[j].Value.ToString();
                    }
            }
        }
        public static void DeleteData(string strTableName,XMLData xmlD,string strElement)
        {
            if (System.Windows.Forms.MessageBox.Show("Bạn có chắc muốn xóa?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                XElement xEmp = XElement.Load(DATA_PATH + strTableName + ".xml");
                //
                var empDetails = from emps in xEmp.Elements(strTableName)
                                 where emps.Element(xmlD.Names[0]).Value.Equals(strElement)
                                 select emps;
                //
                empDetails.First().Remove();
                //
                xEmp.Save(DATA_PATH + strTableName + ".xml");
            }

        }
        public static void ClearControls(System.Windows.Forms.Form frm,XMLData xmlD)
        {
            for (int i = 0; i < frm.Controls.Count; i++)
            {
                if (frm.Controls[i].Tag != null)
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {

                        if (frm.Controls[i].Tag.ToString()==xmlD.Names[j])
                            frm.Controls[i].Text = "";
                    }
            }
        }
        public static void UpdateData(System.Windows.Forms.Form frm,string strTableName, XMLData xmlD, string strElement)
        {
            for (int i = 0; i < frm.Controls.Count; i++)
            {
                if (frm.Controls[i].Tag != null)
                    for (int j = 0; j < xmlD.SoColumn; j++)
                    {

                        if (xmlD.Names[j] == frm.Controls[i].Tag.ToString())
                            xmlD.Value[j] = frm.Controls[i].Text;
                    }
            }
            XElement xEmp = XElement.Load(DATA_PATH + strTableName + ".xml");
            //
            var empDetails = from emps in xEmp.Elements(strTableName)
                             where emps.Element(xmlD.Names[0]).Value.Equals(strElement)
                             select emps;
            foreach (XElement xEle in empDetails)
            {
                for (int i = 0; i < xmlD.SoColumn; i++)
                {
                    xEle.Element(xmlD.Names[i]).Value = xmlD.Value[i];
                }
                
            }
            //
            
            //
            xEmp.Save(DATA_PATH + strTableName + ".xml");
        }
    }
}
